<!-- 信息编辑弹窗 -->
<template>
  <div class="ele-body">
    <a-card :title="staffAttendanceRecordsModalApp.title" :bordered="false">
      <a-form
        ref="form2"
        :model="staffAttendanceRecordsModalApp.staffAttendanceRecords"
        :rules="staffAttendanceRecordsModalApp.rules"
        :label-col="{md: {span: 7}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 13}, sm: {span: 24}}">
        <a-row :gutter="16">
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="员工:" name="staffId">
              <m-entity-select
                :default-value="staffAttendanceRecordsModalApp.staffAttendanceRecords.staffName"
                v-model:value="staffAttendanceRecordsModalApp.staffAttendanceRecords.staffId"
                placeholder="请选择员工"
                module="staff"
                entity="Staff"
                class="ele-fluid"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="状态:" name="status">
              <m-dict-select
                v-model:value="staffAttendanceRecordsModalApp.staffAttendanceRecords.status"
                placeholder="请选择状态"
                dict="出勤状态"/>
            </a-form-item>
          </a-col>
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="出勤时间:" name="date">
              <a-date-picker
                v-model:value="staffAttendanceRecordsModalApp.staffAttendanceRecords.date"
                placeholder="出勤时间"
                value-format="YYYY-MM-DD H:m:s"
                class="ele-fluid"/>
            </a-form-item>
          </a-col>

          <a-col :md="12" :sm="24" :xs="24">
            <a-form-item :wrapper-col="{md: {offset: 6}}" style="margin-bottom: -20px">
              <!--class="ele-pull-right"-->
              <div v-if="staffAttendanceRecordsModalApp.display">
                <teleport to="#staffAttendanceRecordsEditModalFooter">
                  <div>
                    <a-space size="middle">
                      <a-button @click="staffAttendanceRecordsListApp.editModalShowing=false">取消
                      </a-button>
                      <a-button
                        type="primary"
                        @click="onSubmit(staffAttendanceRecordsModalApp.staffAttendanceRecords)"
                        :loading="loading">提交
                      </a-button>
                      <!--<a-button v-if="!eid"
                                type="dashed"
                                @click="continueSubmit"
                                :loading="loading">继续提交
                      </a-button>-->
                    </a-space>
                  </div>
                </teleport>
              </div>
              <div v-if="!staffAttendanceRecordsModalApp.display">
                <a-space size="middle">
                  <a-button @click="onBack()">返回列表</a-button>
                  <a-button
                    type="primary"
                    @click="onSubmit(staffAttendanceRecordsModalApp.staffAttendanceRecords)"
                    :loading="loading">提交
                  </a-button>
                  <!--<a-button v-if="!eid"
                            type="dashed"
                            @click="continueSubmit"
                            :loading="loading">继续提交
                  </a-button>-->
                </a-space>
              </div>

            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
    </a-card>

  </div>

</template>

<script>
import {defineComponent, inject, reactive, onMounted, ref} from 'vue'
import {useRoute, useRouter} from "vue-router"
import {StaffAttendanceRecordsService} from "@/views/restaurant/staff/staffAttendanceRecords/staffAttendanceRecordsService";
import {VXETable} from 'vxe-table'
import {useStore} from "vuex";
import regions from 'ele-admin-pro/packages/regions.js';


import MDictSelect from "@/components/MDict/dictSelect";
import MEntitySelect from "@/components/MEntity/entitySelect";

export default defineComponent({
  components: {
    MDictSelect,
    MEntitySelect,
  },
  setup() {
    // 省市区数据
    const cityData = regions
    const route = useRoute();
    const router = useRouter();
    const store = useStore();
    let routeId = route.params.id;
    let staffAttendanceRecordsListApp = inject('staffAttendanceRecordsListApp', reactive({}));
    let staffAttendanceRecordsGrid = inject('staffAttendanceRecordsGrid',ref())
    const staffAttendanceRecordsModalApp = reactive({
      id: null,
      staffAttendanceRecords: {},
      isEdit: false,
      display: false
    });
    staffAttendanceRecordsModalApp.rules = {
      staffName: [
        {
          required: false,
          type: 'string',
          message: '请输入员工',
          trigger: 'blur'
        }
      ],
      staffId: [
        {
          required: true,
          type: 'number',
          message: '请输入员工',
          trigger: 'blur'
        }
      ],
      date: [
        {
          required: false,
          type: 'string',
          message: '请输入出勤时间',
          trigger: 'blur'
        }
      ],
    }

    onMounted(() => {
      //清理数据
      staffAttendanceRecordsModalApp.staffAttendanceRecords = reactive({});
      if (routeId) {//路由
        staffAttendanceRecordsModalApp.id = routeId;
        staffAttendanceRecordsModalApp.display = false;
        findStaffAttendanceRecords(staffAttendanceRecordsModalApp.id);
      } else if (staffAttendanceRecordsListApp.editModalForEdit) {//修改弹窗
        staffAttendanceRecordsModalApp.id = staffAttendanceRecordsListApp.currentId;
        staffAttendanceRecordsModalApp.display = true;
      } else if (!staffAttendanceRecordsListApp.editModalForEdit && staffAttendanceRecordsListApp.addModalForEdit) {//新增弹窗
        staffAttendanceRecordsModalApp.display = true;
      } else {
        staffAttendanceRecordsModalApp.title = "新增";
      }
      if (staffAttendanceRecordsModalApp.id) findStaffAttendanceRecords(staffAttendanceRecordsModalApp.id);
    })

    const findStaffAttendanceRecords = (id) => {
      StaffAttendanceRecordsService.findStaffAttendanceRecordsForView(id).then((res) => {
        staffAttendanceRecordsModalApp.staffAttendanceRecords = res.data;
        //TODO:为编辑准备.
        // staffAttendanceRecordsModalApp.title = "编辑 " + staffAttendanceRecordsModalApp.staffAttendanceRecords.name;
      }).catch(error => {
        VXETable.modal.message({content: `查找出错，原因是：${error.message}`, status: 'error'});
      })
    }

    /*TODO:提交 新增&编辑*/
    const onSubmit = (data) => {
      // debugger;
      //当为数组时用逗号连接
      if (data.eid) {
        //修改
        StaffAttendanceRecordsService.updateStaffAttendanceRecords(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      } else {
        //新增
        StaffAttendanceRecordsService.saveStaffAttendanceRecords(data).then((res) => {
          console.log(res);
          VXETable.modal.message({content: '操作成功', status: 'success'});
          setTimeout(onBack, 3000);
        }).catch(error => {
          VXETable.modal.message({content: `系统错误，原因是：${error.message}`, status: 'error'});
        })
      }
    }

    /*TODO:继续提交*/
    const continueSubmit = () => {
      VXETable.modal.message({content: 'error 提示', status: 'error'});
    }

    /*TODO:返回列表*/
    const onBack = () => {
      router.push(`/restaurant/staff/staffAttendanceRecords`);
      store.dispatch('user/tabRemove', route.fullPath);
      setTimeout(() => {
        staffAttendanceRecordsGrid.value.commitProxy("query");
      }, 1500);
      staffAttendanceRecordsListApp.editModalShowing = false;
    }
    return {
      cityData,
      routeId,
      staffAttendanceRecordsListApp,
      staffAttendanceRecordsModalApp,
      onSubmit,
      continueSubmit,
      onBack,
    }

  },
})
</script>

<style scoped>

</style>
